<?php
/**
 * KePHP, Keep PHP easy!
 */

namespace CSService\Controller;

use Ke\Adm\Query;
use Ke\Adm\Sql\QueryBuilder;

class Task extends Abs
{

	public $pageSize = 20;

	public $pageCount = 0;

	public $page = 0;

	/** @var Query */
	public $query;

	private function getList()
	{
		$this->page = $this->http->query('page', 1) - 1;
		if (!is_numeric($this->page) || $this->page < 0)
			$this->page = 0;
		$cloneQuery = clone $this->query;
		$recordCount = $cloneQuery->count();
		$this->pageCount = intval($recordCount / $this->pageSize);
		if ($recordCount % $this->pageSize > 0)
			$this->pageCount += 1;
		if ($this->page >= $this->pageCount)
			$this->page = $this->pageCount - 1;
		$this->query->limit($this->pageSize)->offset($this->page * $this->pageSize);
		return $this->query->find();
	}

	public function index()
	{
		$this->query = \CSService\Model\Task\Task::query('list');
		$this->tasks = $this->getList();
	}

	public function log()
	{
		$this->query = \CSService\Model\Task\Task_Log::query('list');
		$this->logs = $this->getList();
	}


	public function log_detail()
	{
		$this->query = \CSService\Model\Task\Task_Log::query('list')->where('tb1.id', 'in', $this->web->tail(0));
		$this->query->addSelect('tb1.misc');
		$this->log = $this->query->findOne();
	}
}